var isModalShown = false;
var modal = document.getElementById('my-modal');
var closeBtn = document.getElementsByClassName("close")[0];

var last_text='';

closeBtn.onclick = function() {
 modal.style.display = "none";
 isModalShown = false;
};

function show_line_data(){
$.ajax({
    url:"/line",
    success:function(data){
        let water = data.moisture_content[data.moisture_content.length - 1];
        let bud = data.tender_bud_num[data.tender_bud_num.length - 1];
        let temp = data.air_temperature[data.air_temperature.length - 1];
        let humidity = data.air_humidity[data.air_humidity.length - 1];
        if(water<=0.6 || temp >= 45) {
            var text=String(data.day[data.day.length - 1]);
            //var audio = new Audio();// 创建音频元素
            //audio.src = "{{ url_for('static', filename='sound/warn.wav') }}";//jinja2允许，单独js不允许
            //audio.play();

            //改为如下
            // 获取音频元素和源元素
            let bg_music = document.getElementById("bg_music");
            let bg_music_src = document.getElementById("bg_music_src");

            // 设置音频文件的URL
            bg_music_src.src = "/static/sound/warn.wav"; // 将路径地址替换为实际的音频文件名称和路径

            // 加载音频文件后自动播放
            bg_music.load();
            bg_music.play();

            if (water<=0.6) {
               text=text+"含水率达到"+water+"，请尽快结束萎凋！"
            }

            if (temp >= 45) {
               text=text+"温度达到"+temp+"°C，请及时降温！"
            }

//                              else {
//                                return;
//                              }
            document.getElementById("my-modal").querySelector("p").textContent = text;

            if(last_text!==text){
                last_text=text;
                const xhr = new XMLHttpRequest(); // 创建 HTTP 请求对象
                xhr.open('POST', '/accept_warn', true); // 发送请求
                xhr.setRequestHeader('Content-Type', 'application/json'); // 设置请求头
                xhr.send(JSON.stringify({warn: text})); // 发送数据
            }

            modal.style.display = "block";
            isModalShown = true;
        }

    },error:function(xhr,type,errorThrown){

    }
})

let content = modal.querySelector(".modal-content");
let isDragging = false;// 是否在移动弹窗
let positionX = 0; // 点击位置与弹窗左侧的距离
let positionY = 0; // 点击位置与弹窗顶部的距离

content.addEventListener("mousedown", function(e) {
isDragging = true;
positionX = e.pageX - content.offsetLeft;
positionY = e.pageY - content.offsetTop;
});

document.addEventListener("mousemove", function(e) {
if (isDragging) {
  let x = e.pageX - positionX;
  let y = e.pageY - positionY;

  // 控制弹窗不移出窗口边界
//                  let maxLeft = window.innerWidth - content.offsetWidth;
//                  let maxTop = window.innerHeight - content.offsetHeight;
//                  if (x < 0) x = 0;
//                  if (x > maxLeft) x = maxLeft;
//                  if (y < 0) y = 0;
//                  if (y > maxTop) y = maxTop;

  content.style.left = x + "px";
  content.style.top = y + "px";
}
});

document.addEventListener("mouseup", function(e) {
isDragging = false;
});

document.querySelector(".close").addEventListener("click", function() {
modal.style.display = "none";
});


// 移动设备上的 touch 事件
content.addEventListener("touchstart", (e) => {
positionX = e.touches[0].clientX - content.offsetLeft;
positionY = e.touches[0].clientY - content.offsetTop;

isDragging = true;
});


content.addEventListener("touchend", () => {
isDragging = false;
});

content.addEventListener("touchmove", (e) => {
if (isDragging) {
  e.preventDefault();

  const currentX = e.touches[0].clientX - positionX;
  const currentY = e.touches[0].clientY - positionY;

  content.style.left = `${currentX}px`;
  content.style.top = `${currentY}px`;
}
});

// 阻止默认行为和事件冒泡
content.addEventListener("click", function(e) {
e.stopPropagation();
});
modal.addEventListener("click", function(e) {
modal.style.display = "none";
});

}


show_line_data();
setInterval(show_line_data,3000);
setInterval(function() {
if (isModalShown) {
    modal.style.display = "none";
    isModalShown = false;
  }
}, 5000);
